package me.firstapp.repository.shop.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Repository;

import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.shop.Sku;
import me.firstapp.repository.base.impl.EntityRepositoryImpl;
import me.firstapp.repository.shop.SkuRepository;

@Repository
public class SkuRepositoryImpl extends EntityRepositoryImpl<Sku> implements SkuRepository {

	@Override
	public Page<Sku> findSkuPage(Integer pageNo, Integer pageSize, Map<String, Object> searchParams, String sortParam,
			String sortType, Long apiClientId) {
		StringBuilder sql = new StringBuilder(" FROM Sku bean where 1=1 ");
		Map<String, Object> parameters = new HashMap<>();

		if (searchParams != null) {
			Set<String> keys = searchParams.keySet();
			for (String key : keys) {
				Object value = searchParams.get(key);
				if (value != null) {
					if ("productId".equals(key)) {
						Integer productIdInteger = (Integer) value;
						Long productId = Long.valueOf(productIdInteger);
						sql.append(" AND bean.productId =:productId ");
						parameters.put("productId", productId);
					} else {
						sql.append(" AND bean." + key + " LIKE :value ");
						parameters.put("value", "%" + value + "%");
					}
				}
			}
		}

		sql.append(" AND bean.apiClientId =:apiClientId ");
		parameters.put("apiClientId", apiClientId);

		if (StrUtils.isNotNULL(sortParam) && StrUtils.isNotNULL(sortType)) {
			sql.append(" ORDER BY " + sortParam + " " + sortType);
		}

		return super.queryForPage(Sku.class, sql.toString(), parameters, pageNo, pageSize);
	}

	@Override
	protected Class<Sku> getPersistentClass() {
		return Sku.class;
	}

	@Override
	public Sku findSkuByProductIdAndSkuName(Long productId, String name, Long apiClientId) {
		StringBuilder sql = new StringBuilder(" FROM Sku bean where 1=1 ");
		Map<String, Object> parameters = new HashMap<>();

		sql.append(" AND bean.apiClientId =:apiClientId ");
		parameters.put("apiClientId", apiClientId);

		sql.append(" AND bean.productId =:productId ");
		parameters.put("productId", productId);

		sql.append(" AND bean.name =:name ");
		parameters.put("name", name);

		List<Sku> result = super.query(Sku.class, sql.toString(), parameters);
		return result.isEmpty() ? null : result.get(0);
	}

	@Override
	public List<Sku> findSkusByProductId(Long productId, Long apiClientId) {
		StringBuilder sql = new StringBuilder(" FROM Sku bean where 1=1 ");
		Map<String, Object> parameters = new HashMap<>();

		sql.append(" AND bean.apiClientId =:apiClientId ");
		parameters.put("apiClientId", apiClientId);

		sql.append(" AND bean.productId =:productId ");
		parameters.put("productId", productId);

		return super.query(Sku.class, sql.toString(), parameters);
	}

	@Override
	public Sku findById(Long id, Long apiClientId) {
		StringBuilder sql = new StringBuilder(" FROM Sku bean where 1=1 ");
		Map<String, Object> parameters = new HashMap<>();

		sql.append(" AND bean.apiClientId =:apiClientId ");
		parameters.put("apiClientId", apiClientId);

		sql.append(" AND bean.id =:id ");
		parameters.put("id", id);

		List<Sku> result = super.query(Sku.class, sql.toString(), parameters);
		return result.isEmpty() ? null : result.get(0);
	}

}
